<!DOCTYPE html>
<html>

<head>
<title>减重管理</title>
<meta http-equiv="Pragma" CONTENT="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="renderer" content="webkit">
<%include("/common/include.html"){}%>
</head>

<body class="easyui-layout">
		<div data-options="region:'north',border: false" style="height:60px">
			<%include("/customer/searchCustomer4.html"){}%>
		</div>
		<div id="tb" style="height: auto">
						<a href="#" onclick='addTarget()' class="easyui-linkbutton" iconCls="icon-add">添加月目标</a>
						<a id="edit" href="#" class="easyui-linkbutton" iconCls="icon-edit" onclick="upTarget()">修改</a>
						<a id="delete" href="#" class="easyui-linkbutton" iconCls="icon-edit" onclick="deleteTarget()">删除</a>
					</div>
		<div data-options="region:'center',border: false" style="height:auto;">
			
			<table id="targetTable" style="height: auto">

					</table>
					<table id="upTargetTable" style="height: auto">

					</table>

				
		</div>
	
<script type="text/javascript">
				function addTarget(){
					var customerIds = $("#customerIds").val();
					loadDialog($("#targetTable"), null, '添加目标', 'bloseWeight/toAddTarget?ids=' + customerIds);
				}
				function upTarget(){
					 var row = $("#targetTable").datagrid('getSelected');
					 if(row == null){
						 $.slideMsg("请选中一条记录！", parent.$);
					 }else if(row.targetname =="总目标"){
						 $.alert(parent.$,"总目标不能修改");
					 }
					 else{
						 loadDialog($("#targetTable"), null, '修改目标', 'bloseWeight/toUpTarget?ids=' + row.ids);
					 }
				}
				function deleteTarget(){
					 var row = $("#targetTable").datagrid('getSelected');
					 if(row == null){
						 $.slideMsg("请选中一条记录！", parent.$);
					 }else if(row.targetname =="总目标"){
						 $.alert(parent.$,"总目标不能删除");
							
					 }
					 else{
						 //loadDialog($("#targetTable"), null, '修改目标', 'bloseWeight/deleteTarget?ids=' + row.ids);
						//保存除宣传品以外的信息
							$.ajax({
				                cache: true,
				                type: "GET",
				                url:"${cxt!}/bloseWeight/deleteTarget?ids=" + row.ids,
				               // data:$('#propaganda').serialize(),// 你的formid
				              //  dataType:'json',
				                async: false,
				                success: function(result) {		                	
				                	if(result.code == "200")
				                	{	
				                		$.slideMsg("保存成功", parent.$);
				                		 $('#targetTable').datagrid('reload');
				                		
				                		
				                	}else{		                		
				        					 $.alert(parent.$,result.msg);
				                	}
				                }
				            });
					 }
				}
				function initTargetSumTable(){
					var customerIds = $("#customerIds").val();
					if(customerIds == '' || typeof(customerIds)=="undefined"){
						customerIds = '0';
					}
					$('#targetTable').datagrid({
						cls: "theme-datagrid",
						toolbar: '#tb',
						singleSelect: true,
						fitColumns: true,
						rownumbers: true, //显示序号
						pagination: true,
						pageSize: 10,
						url: "${cxt!}/bloseWeight/targetList",
						queryParams: {
							'_query.customerIds': customerIds
						},
					    method: 'post',
						collapsible: true,
						columns:[[
							  		{field:'ids',title:'ids',width:100,align:'center', hidden:true},
							  		{field:'customerids',title:'customerids',width:100,align:'center',hidden:true},
							  		{field:'codeIds',title:'codeIds',width:100, align:'center',hidden:true},
							  		{field:'targetname',title:'名称',width:100, align:'center',
	    								formatter: function(value,row,index){
											var ids = row.ids;
											if(row.targetname == "总目标"){
												return  value ;
											}else{
												return '<a href="#" style="color: blue;" class="button-edit" onclick="initTargetTable(\'' + ids + '\')">' + value + '</a>';
											}
											
										}},
							  		{field:'starttime',title:'开始时间',width:160, align:'center'},
							  		{field:'endtime',title:'结束时间',width:160, align:'center'},
							  		{field:'startwei',title:'初始体重',width:160, align:'center',
	    								formatter: function(value,row,index){
											var startwei = row.startwei;
											startwei = parseFloat(startwei);
											startwei = startwei.toFixed(1);
											return startwei;
											
										}},
							  		{field:'targetwei',title:'目标体重',width:160, align:'center',
		    								formatter: function(value,row,index){
												var targetwei = row.targetwei;
												targetwei = parseFloat(targetwei);
												targetwei = targetwei.toFixed(1);
												return targetwei;
												
											}},
							  		{field:'delwei',title:'应减体重',width:160, align:'center',
			    								formatter: function(value,row,index){
													var delwei = row.delwei;
													delwei = parseFloat(delwei);
													delwei = delwei.toFixed(1);
													return delwei;
													
												}}
							      ]],
						onLoadSuccess:function(){
						}
					});
				}
				
			
				//初始化目标table
				function initTargetTable(targetSumIds){
					$('#upTargetTable').datagrid({
						cls: "theme-datagrid",
						
						singleSelect: true,
						fitColumns: true,
						rownumbers: true, //显示序号
						url: "${cxt!}/bloseWeight/getTargets?ids=" + targetSumIds,
					    method: 'post',
						columns:[[	
									{field:'ids',title:'ids',width:100,align:'center', hidden:true},
									{field:'schedule',title:'排期',width:100,align:'center'},
									{field:'starttime',title:'起始日期',width:100,align:'center'},
									{field:'endtime',title:'截止日期',width:100,align:'center'},
									{field:'plantostorenum',title:'计划到店次数',width:100,align:'center',editor:'numberspinner'},
									{field:'delweitarget',title:'减重目标（斤）',width:100,align:'center',editor:'numberspinner'}
							    ]],
						onLoadSuccess:function(){
				        	
						},
						onClickRow:function(index, rowData){
							if (editIndex != index){
								if (endEditing()){
									$('#upTargetTable').datagrid('selectRow', index)
											.datagrid('beginEdit', index);
									editIndex = index;
								} else {
									$('#upTargetTable').datagrid('selectRow', editIndex);
								}
							}
						}
					});
				}
				
				//获取顾客信息
				function searchCus()
				{	
					var reData=getCusInfo();
					if(reData.msg==200)
					{
						initTargetSumTable();
					}
				}
				//清除筛选条件
				function clearsj(){
					
					location.reload();
				}
				
				$(function() {
					initTargetSumTable();
				})
				$(document).keydown(function(event){ 
					if(event.keyCode == 13){
						var hybh=$('#customerHybh').val();
						var name=$('#customerName').val();
						var phone=$('#customerPhone').val();
						$("#customerHybh").textbox('setValue',hybh);
						
						if(hybh !="" || name !="" || phone !="")
						{
						
							var url='${cxt!}/bentryRegister/getCusByHybh';
							var data={"hybh":hybh,"name":name,"phone":phone};
							var reData = common_ajax.ajaxFunc(url,data);
							var daJson=$.parseJSON(reData);
							var msg=daJson.msg;
							if(msg=="200")
							{
								$("#customerIds").val(daJson.customer.ids);
								$("#customerName").textbox('setValue',daJson.customer.name);
								$("#customerPhone").textbox('setValue',daJson.customer.phone);
								$("#customerHybh").textbox('setValue',daJson.customer.hybh);
								initTargetSumTable();
								
							}else if(msg=="201")
							{
								 $.alert(parent.$, "查询失败，请重新输入查询条件");
							}else
							{
								 $.alert(parent.$,msg);
							}
						
						}
					} 
				}); 
			</script>

</body>